'use strict';

let { Blog, Op } = require('../model');

//查找
let fn_list = async (ctx, next) => {
    let keyword = ctx.request.query.keyword || '';
    console.log(keyword);
    let list = await Blog.findAll(
        {
            order: ['id']
        }
    )
    if (keyword) {
        list = await Blog.findAll({
            where: {
                [Op.or]: [

                    { id: isNaN(keyword) ? 0 : parseInt(keyword) },
                    { biaoti: keyword },
                    { zhaiyao: keyword },
                    { neirong: keyword },
                    { fenlei: keyword },
                    { zuozhe: keyword },
                    { fabiaoshijian: keyword },
                ]
            },
            order: ['id']
        })


    }


    ctx.body = list
}




//保存
let save = async (ctx, next) => {
    let obj = ctx.request.body
    // console.log(obj);
    await Blog.create(obj)
    ctx.body = ''
}








//添加
let fn_post = (ctx, next) => {
    let obj = ctx.request.body;
    console.log(obj);
    obj.id = blog[blog.length - 1].id + 1;
    console.log(obj);
    blog.push(obj);
    ctx.body = obj;
}



//删除
let fn_delete = async (ctx, next) => {
    let id = ctx.request.body.id
    console.log(id);

    let dele = await Blog.destroy({
        where: {
            id: id
        }
    })
    ctx.body = dele
}




module.exports = {
    'get /Blog': fn_list,
    'post /save': save,
    'post /Blog': fn_post,
    'post /del': fn_delete
}